<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

require_once 'Classes/Controllers/Controller.php';
require_once 'Classes/Model/LoanModel.php';

class LoanController extends Controller {

    public function processRequest() {
        switch (HTTPRequestModel::getInstance()->extractGETValue('action')){
            case 'AddLoan':
                $this->addLoan();
                break;
            case 'EditLoan':
                $this->editLoan();
                break;
	    case 'DeleteLoan':
                $this->deleteLoan();
            default:
                $this->listLoans();
                break;
        }
    }
    
    public function addLoan(){
        
         if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "post"){
	     
	     $id= HTTPRequestModel::getInstance()->extractPOSTValue('ID');
	     $statut= HTTPRequestModel::getInstance()->extractPOSTValue('Statut');
	     $startingDate= HTTPRequestModel::getInstance()->extractPOSTValue('Starting_Date');
	     $endingDate= HTTPRequestModel::getInstance()->extractPOSTValue('Ending_Date');
	     $clientID= HTTPRequestModel::getInstance()->extractPOSTValue('Client_ID');
	     
	  
	     if($statut!="" && $startingDate!="" && $endingDate!="" && $clientID!=""){
                
		$loan=new Loan($id,$statut,$startingDate,$endingDate,$clientID);
		LoanModel::getInstance()->addLoan($loan);
                header('Location: /');		 
            }	    	     	     	     	       
         }
         else if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "get") {
            require_once 'Classes/View/Pages/FormBasedPage.php';

            $this->view = new FormBasedPage("New Loan");
            $this->view->addCSS('/Ressources/style.css');
            $this->view->addElement(new Loan());
            $this->view->display();
         }
        
    }
    
    public function editLoan(){
         if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "post"){
	     
	     $id= HTTPRequestModel::getInstance()->extractPOSTValue('ID');
	     $statut= HTTPRequestModel::getInstance()->extractPOSTValue('Statut');
	     $startingDate= HTTPRequestModel::getInstance()->extractPOSTValue('Starting_Date');
	     $endingDate= HTTPRequestModel::getInstance()->extractPOSTValue('Ending_Date');
	     $clientID= HTTPRequestModel::getInstance()->extractPOSTValue('Client_ID');
	     
	  
	     if($statut!="" && $startingDate!="" && $endingDate!="" && $clientID!=""){
                
		$loan=new Loan($id,$statut,$startingDate,$endingDate,$clientID);
		LoanModel::getInstance()->updateLoan($loan);
                header('Location: /');
		 
            }
             
         }
         else if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "get") {
            require_once 'Classes/View/Pages/FormBasedPage.php';
            
            $this->view = new FormBasedPage("Edit Loan");
            $this->view->addCSS('/Ressources/style.css');
            $loanId = HTTPRequestModel::getInstance()->extractGETValue('Loan');
            $loan = LoanModel::getInstance()->getLoanById($loanId);
            $this->view->addElement($loan);
            $this->view->display();
         }
     }
     
     public function listLoans(){
         require_once 'Classes/View/Pages/ListBasedPage.php';
         if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "post"){
             
	      $id= HTTPRequestModel::getInstance()->extractPOSTValue('ID');
	     $statut= HTTPRequestModel::getInstance()->extractPOSTValue('Statut');
	     $startingDate= HTTPRequestModel::getInstance()->extractPOSTValue('Starting_Date');
	     $endingDate= HTTPRequestModel::getInstance()->extractPOSTValue('Ending_Date');
	     $clientID= HTTPRequestModel::getInstance()->extractPOSTValue('Client_ID');
	     
	  
	     if($statut!="" && $startingDate!="" && $endingDate!="" && $clientID!=""){
                
		$loan=new Loan($id,$statut,$startingDate,$endingDate,$clientID);
		LoanModel::getInstance()->getAllLoan($loan);
                header('Location: /');
		 
            }
	     
         }
         else if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "get") {

            $this->view = new ListBasedPage("Loans");
            $this->view->addCSS('/Ressources/style.css');
            $this->view->addElement(LoanModel::getInstance()->getAllLoans());
            $this->view->display();
         }
     }
     
     public function deleteLoanById(){
	$loadId =HTTPRequestModel::getInstance()->extractGETValue('loan');
         LoanModel::getInstance()->deleteLoanById($loanId);
         
         header('Location: /');
     }
    
}

?>
